$(document).ready(function () {
    //从页面文本框中取出主键
    var cid = getQueryVariable("cid");
    $.ajax({
        "url": "customerController/customer/"+cid+"/json",
        "method": "get",
       // "data": {"mno": mno},
        "dataType": "json",
        "success": function (dto) {
            // alert(dto);
            console.log(dto);
            var customer = dto.t;
            showAttribute(customer,$("#info ul li span"));
            console.log(customer.employrecords);
            var dietitians = new Array();//造一个营养师空数组
            //显示有合同关联的营养师
            $(customer.employrecords).each(function (i,employrecord) {
                var dietitian = employrecord.dietitian;
                var isExits = false;
                for (var i in dietitians) {
                    //console.log(dietitians[i].did + " "+ did);
                    if (dietitians[i].did == dietitian.did) {
                        isExits = true;
                        break;
                    }
                }
                if (!isExits) {
                    dietitians.push(dietitian);
                }
            })
             console.log(dietitians);
            showListAttribute(dietitians,$("#dietitians ul li").eq(0));
            //显示我的合同

            showListAttribute(customer.employrecords,$("#employrecords ul li").eq(0));
        }
    });


});
function showAttribute(item,$spans) {
    $.each(item, function (key) {
        var attrName = key;
        var attrVal = item[key];
        //console.log(attrName +" "+ attrVal);
        //如果json对象的属性的名字和元素的atl的值一致就赋值
        $spans.each(function(i,span){
            var $span = $(span);//将js对象变成jquery对象
            var alt = $span.attr('alt');
            if(alt!='undifined'&&alt!=''&&alt!=null&&alt==attrName){
                if ($span.attr('isdate')){
                    attrVal = formatDate(attrVal);
                }
                $span.text(attrVal);
            }
        });


    });
}
function showListAttribute(items,$itemTemplate) {
    //用于保存通过clone得到静态html元素 通过js替换成返回的数据填充的html
    for (var i in items) {//将传入json对象集合循环出来
        var item = items[i];
        var $itemHtml = $itemTemplate.clone();
        //使用双重循环查找 返回的对象的值 和html元素的属性是否匹配
        $.each(item, function (key) {
            var attrName = key;
            var attrVal = item[key];
            //console.log(attrName + " " + attrVal);
            //如果json对象的属性的名字和元素的atl的值一致就赋值
            var $spans = $itemHtml.find('span');//因为有多个克隆对象 每个克隆对象 都带有很多span
            $spans.each(function (i, span) {
                var $span = $(span);//将js对象变成jquery对象
                var alt = $span.attr('alt');
                if (alt != 'undifined' && alt != '' && alt != null && alt == attrName) {

                    if ($span.attr('isdate')){//如果标签上面有isDate=true 就格式日期之后再绑定
                        attrVal = formatDate(attrVal);
                    }
                    $span.text(attrVal);
                }
            });
//循环结束意味着clone对象已经准备好，添加到模板对象的父元素的尾部
            $itemTemplate.parent().append($itemHtml);
        });

    }
    $itemTemplate.remove();
}